package com.queue;


import org.junit.Test;
import sun.nio.ch.ThreadPool;

import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.TimeUnit;

/**
 * 阻塞队列
 * BlockingQueue四组API
 */
public class ArrayBlockingQueueDemo {

    public static void main(String[] ars) throws Exception {
//        test1();
//        test2();
//        test3();
//        test4();
    }

    /**
     * 抛出异常
     */
    public static void test1() {
        java.util.concurrent.ArrayBlockingQueue arrayBlockingQueue = new java.util.concurrent.ArrayBlockingQueue<>(3);
        System.out.println(arrayBlockingQueue.add("A"));
        System.out.println(arrayBlockingQueue.add("b"));
        System.out.println(arrayBlockingQueue.add("c"));
        System.out.println("-------------------------------------------------");

        System.out.println(arrayBlockingQueue.remove());
        System.out.println(arrayBlockingQueue.remove());
        System.out.println(arrayBlockingQueue.remove());
        System.out.println(arrayBlockingQueue.element());  //返回队首元素

    }


    /**
     * 有返回值，没有异常
     */
    public static void test2() {
        java.util.concurrent.ArrayBlockingQueue arrayBlockingQueue = new java.util.concurrent.ArrayBlockingQueue<>(3);

        System.out.println(arrayBlockingQueue.offer("A"));
        System.out.println(arrayBlockingQueue.offer("b"));
        System.out.println(arrayBlockingQueue.offer("C"));
        System.out.println(arrayBlockingQueue.offer("C"));  //超过指定大小则会返回false

        System.out.println(arrayBlockingQueue.peek());  //检测队首元素
        System.out.println(arrayBlockingQueue.poll());
        System.out.println(arrayBlockingQueue.poll());
        System.out.println(arrayBlockingQueue.poll());
        System.out.println(arrayBlockingQueue.poll());  //取完之后会返回null

    }

    /**
     * 等待，阻塞（一直阻塞）
     */
    public static void test3() throws InterruptedException {
        java.util.concurrent.ArrayBlockingQueue arrayBlockingQueue = new java.util.concurrent.ArrayBlockingQueue<>(3);

        arrayBlockingQueue.put("a");
        arrayBlockingQueue.put("b");
        arrayBlockingQueue.put("c");
//        arrayBlockingQueue.put("c");  //队列没有位置，一直阻塞

        //取出
        System.out.println(arrayBlockingQueue.take());
        System.out.println(arrayBlockingQueue.take());
        System.out.println(arrayBlockingQueue.take());
        System.out.println(arrayBlockingQueue.take()); //队列没有值，则一直阻塞，等待，不会报错
    }

    /**
     * 等待，超时退出
     */
    @Test
    public void test4() throws InterruptedException {
        java.util.concurrent.ArrayBlockingQueue arrayBlockingQueue = new java.util.concurrent.ArrayBlockingQueue<>(3);

        System.out.println(arrayBlockingQueue.offer("a"));
        System.out.println(arrayBlockingQueue.offer("b"));
        System.out.println(arrayBlockingQueue.offer("c"));
        arrayBlockingQueue.take();
//        System.out.println(arrayBlockingQueue.offer("d", 2, TimeUnit.SECONDS));

        System.out.println("===========================");

        System.out.println(arrayBlockingQueue.poll());
        System.out.println(arrayBlockingQueue.poll());
        System.out.println(arrayBlockingQueue.poll());
        System.out.println(arrayBlockingQueue.poll(2, TimeUnit.SECONDS));//取出，没有元素则等待2秒钟
    }

    @Test
    public void test5() throws InterruptedException {
        boolean aTrue = Boolean.parseBoolean("tre");
        System.out.println(aTrue);


        LinkedHashMap<String, Object> linkedHashMap = new LinkedHashMap<>();
        linkedHashMap.put("合同类型", 1);
        linkedHashMap.put("状态", 2);
        linkedHashMap.put("政策编号", 3);
        linkedHashMap.put("政策名称", 4);

        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("合同类型", 1);
        hashMap.put("状态", 2);
        hashMap.put("政策编号", 3);
        hashMap.put("政策名称", 4);

        System.out.println("---");

    }

    @Test
    public void test6() throws InterruptedException {

    }
}
